package com.zusmart.core.plugin.support;

import java.lang.annotation.Annotation;
import java.util.LinkedHashMap;
import java.util.Map;

import com.zusmart.base.util.AnnotationUtils;
import com.zusmart.core.bean.BeanDefinitionRegistry;
import com.zusmart.core.plugin.Pluggable;
import com.zusmart.core.plugin.Plugin;

public class DefaultPluginManager extends AbstractPluginManager {

	public DefaultPluginManager(BeanDefinitionRegistry beanDefinitionRegistry) {
		super(beanDefinitionRegistry);
	}

	@Override
	public Map<Class<? extends Plugin>, Annotation> resolvePlugins(Class<?> beanType) {
		Map<Class<? extends Plugin>, Annotation> result = new LinkedHashMap<Class<? extends Plugin>, Annotation>();
		if (null == beanType) {
			return result;
		}
		Annotation[] annotations = beanType.getAnnotations();
		if (null == annotations || annotations.length == 0) {
			return result;
		}
		for (int i = 0; i < annotations.length; i++) {
			Annotation annotation = annotations[i];
			Class<?> annotationClass = annotation.annotationType();
			if (!AnnotationUtils.hasAnnotation(annotationClass, Pluggable.class)) {
				continue;
			}
			Pluggable pluggable = AnnotationUtils.getAnnotation(annotationClass, Pluggable.class);
			Class<? extends Plugin> pluginClass = pluggable.value();
			if (null == pluginClass) {
				continue;
			}
			result.put(pluginClass, annotation);
		}
		return result;
	}

}